import Vue from 'vue'
import Router from "vue-router";


Vue.use(Router)



let routes = new Router({
    base:'/love/',
    mode:'history',
    routes:[
        {
            path:'/',
            redirect: '/index'
        },
        // 系统登录
        {
            path:'/login',
            name:'login',
            mate:{
                requireAuth: true
            },
            component:()=>import('../components/user/Login')
        },
        //重置密码
        {
            path:'/retrieve',
            name:'retrieve',
            component:()=>import('../components/user/retrieve.vue')
        },
        // 系统注册
        {
            path:'/register',
            name:'register',
            component:()=>import('../components/user/register')
        },
        // 系统首页
        {
            path:'/index',
            name:'index',
            component:()=>import('../components/user/index')
        },
        // 意见反馈页面
        {
            path:'/advice',
            name:'advice',
            component:()=>import('../components/user/advice')
        },
        // 管理员登陆
        {
            path: '/admin',
            name:'admin',
            component:()=>import('../components/admin/admin'),
        },
        // 内容审核机制
        {
            path:'/admin/contentReview',
            name:'contentReview',
            component:()=>import('../components/admin/contentReview')
        },
        // 用户意见查看与处理
        {
            path:'/admin/suggestion',
            name:'suggestion',
            component:()=>import('../components/admin/suggestion')
        },
        // 404页面
        {
            path:'*',
            component:()=>import('../views/NotFound')
        },
        // 测试
        {
            path:'/test',
            name:'test',
            component:()=>import('../components/test/test.vue')
        },
    ]
});

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

routes.beforeEach((to, from, next) => {
    if (to.matched.some(m=>m.meta.requireAuth)){
        // todo 路由拦截: 以后有时间记得解决这个bug 2021年9月13日by wyb
        console.log("路由拦截");

        if (window.localStorage.token&& window.localStorage.isLogin === 'true'){
            next();
        }else if (to.path !=='/login'){
            let token=window.localStorage.token;
            if (token==='null'||token===''||token===undefined){
                next({path:'/login'})
                console.log("请登录")
            }
        }else {//执行页面跳转
            next()
        }
    }else {//不需要登录
        next()
    }
})


/*
routes.beforeEach(({name}, from, next) => {
    // 获取 JWT Token
    if (localStorage.getItem('JWT_TOKEN')) {
        // 如果用户在login页面
        if (name === 'login') {
            next('/');
        } else {
            next();
        }
    } else {
        if (name === 'login') {
            next();
        } else {
            next({name: 'login'});
        }
    }
});
*/
export default routes